//
//  SYKaoShiBanJiView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/26.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYKaoShiBanJiView.h"
#import "SYKaoShiSelectTableViewCell.h"


@interface SYKaoShiBanJiView () <UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *contView;
@property (weak, nonatomic) IBOutlet UITableView *curTableView;
@property (strong, nonatomic) SYKaoShiBanJiView *banjiView;
@property (assign, nonatomic) NSInteger index;
@property (strong, nonatomic) NSMutableArray *dataArr;

@end



@implementation SYKaoShiBanJiView


- (void)awakeFromNib {
    [super awakeFromNib];
    self.contView.layer.masksToBounds = YES;
    self.contView.layer.cornerRadius = 5;
    self.curTableView.delegate = self;
    self.curTableView.dataSource = self;
    [_curTableView registerCellWithNIbNames:@[@"SYKaoShiSelectTableViewCell"]];
    _curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.index = -1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYKaoShiSelectTableViewCell *xueCell = [tableView dequeueReusableCellWithIdentifier:@"SYKaoShiSelectTableViewCell" forIndexPath:indexPath];
    SYExamInfoClassesModel *classesModel = self.dataArr[indexPath.row];
    if (self.index == -1) {
        
        if (classesModel.isSelect == YES) {
            xueCell.selectImage.image = [UIImage imageNamed:@"yj_xuanze_xuesheng"];
        } else {
            xueCell.selectImage.image = [UIImage imageNamed:@"yj_xuanze_noxues"];
        }
        
    } else {
        if (indexPath.row == _index) {
            xueCell.selectImage.image = [UIImage imageNamed:@"yj_xuanze_xuesheng"];
        } else {//yj_xuanze_noxues
            xueCell.selectImage.image = [UIImage imageNamed:@"yj_xuanze_noxues"];
        }
    }
    xueCell.nameLab.text = classesModel.className;
    xueCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return xueCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataArr.count == 1) {
        return;
    }
    _index = indexPath.row;
    [self.curTableView reloadData];
}

//取消
- (IBAction)cancelBtn:(UIButton *)sender {
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:kAnimate animations:^{
        weakSelf.banjiView.frame = CGRectMake(0, kScreen_HEIGHT, kScreen_WIDTH, kScreen_HEIGHT);
    } completion:^(BOOL finished) {
        [weakSelf.banjiView removeFromSuperview];
    }];
}
//确定
- (IBAction)quedingBtnClick:(UIButton *)sender {
    if (self.index == -1) {//默认选中第一个
        self.index = 0;
    }
    SYExamInfoClassesModel *classModel = self.dataArr[self.index];
    if (self.block) {
        self.block(classModel.classId, classModel);
    }
    [self cancelBtn:sender];
    
}
- (void)showKaoShiBanJi:(SYKaoShiBanJiView *)kaoshiView dataArr:(NSMutableArray *)dataArr{
    _banjiView = kaoshiView;
    _dataArr = dataArr;
    UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
    kaoshiView.frame = CGRectMake(0, kScreen_HEIGHT, kScreen_WIDTH, kScreen_HEIGHT);
    [window addSubview:kaoshiView];
    [UIView animateWithDuration:kAnimate animations:^{
        kaoshiView.frame = CGRectMake(0, 0, kScreen_WIDTH, kScreen_HEIGHT);
    } completion:^(BOOL finished) {
        
    }];
    [self.curTableView reloadData];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:kAnimate animations:^{
        weakSelf.banjiView.frame = CGRectMake(0, kScreen_HEIGHT, kScreen_WIDTH, kScreen_HEIGHT);
    } completion:^(BOOL finished) {
        [weakSelf.banjiView removeFromSuperview];
    }];
}

@end
